package com.yulongtian.oneProblemEveryDay.month202303;


import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * https://leetcode.cn/problems/making-file-names-unique/
 *  参考 可
 * @author yulongTian
 * @create 2023-03-03 10:07
 */
public class Test02 {
    public static void main(String[] args) {
        String[] names = {"wano", "wano", "wano", "wano"};
        System.out.println(Arrays.toString(getFolderNames(names)));
    }

    //可  超时 32 / 33 个通过测试用例
    public static String[] getFolderNames(String[] names) {
        Map<String, Integer> map = new HashMap<>();
        String[] ret = new String[names.length];
        int index = 0;
        for (String name : names) {
            Integer count = map.get(name);
            if (count != null) {
                int k = count + 1;
                while (true) {
                    String newName = name + "(" + k + ")";
                    if (map.get(newName) == null) {
                        map.put(name, k);
                        map.put(newName, 0);
                        ret[index++] = newName;
                        break;
                    }
                    k++;
                }

            } else {
                map.put(name, 0);
                ret[index++] = name;
            }
        }
        return ret;
    }


}
